package com.scalar.chiptrack.operations.shippingRequest.actionCommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.Address;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.shippingRequest.ShippingRequest;
import com.scalar.chiptrack.operations.shippingRequest.dao.ShippingRequestDAO;
import com.scalar.chiptrack.operations.shippingRequest.forms.ShippingRequestForm;
import com.scalar.chiptrack.operations.vendor.dao.ShipperDAO;
import com.scalar.chiptrack.users.User;

/**
 * Created by ChandraSekahr.
 * User: Administrator
 * Date: Nov 21, 2005
 * Time: 11:19:10 AM
 */
public class ShippingRequestModifyCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/shippingRequest.do";
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        ShippingRequestForm shippingRequestForm     = ( ShippingRequestForm ) form;
        ShippingRequest shippingRequest             = shippingRequestForm.getShippingRequest();
        ActionForward actionForward                 = mapping.findForward( "modify" );
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        HashMap errors                              = new HashMap();
        HashMap info                                = new HashMap();
        int shippingRequestId                       = -1;
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }

        try
        {
            try{
                shippingRequestId = Integer.parseInt( request.getParameter("shipReqId") );
            }catch( NumberFormatException nfe)
            {
                shippingRequestId = -1;
            }

            //shippingRequest  = new ShippingRequest();
            shippingRequest.setShipRequestId( shippingRequestId );
            shippingRequest = ShippingRequestDAO.loadShipRerquestDetails( null, shippingRequest );
            shippingRequest.setFromVendorName( ShippingRequestDAO.getVendorName( shippingRequest.getFromLocationId() ) );
            //retriving From VENDOR ADDDRESS AND EMAIL
            Address fromAddress = ShippingRequestDAO.getVendorAddress( shippingRequest.getFromLocationId() ) ;
            if ( fromAddress != null )
            {
                shippingRequest.setFromVendorAddress( fromAddress );
            }
            //retriving From VENDOR ADDDRESS AND EMAIL
            Address toAddress = ShippingRequestDAO.getVendorAddress( shippingRequest.getShipTolocationId() ) ;
            if ( toAddress != null )
            {
                shippingRequest.setToVendorAddress( toAddress );
            }
            //SHOW ALSO TO VENDOR ADDRESS AND EMAIL
            shippingRequest.setToVendorName( ShippingRequestDAO.getVendorName( shippingRequest.getShipTolocationId() ) );
            shippingRequest.setLotList( ShippingRequestDAO.getLotDetails( shippingRequest ) );
            shippingRequest.setShippersList( ShipperDAO.loadShippersForVendor( null, ShippingRequestDAO.getVendorIdByLocationId( shippingRequest.getFromLocationId() ) ) );
            ShippingRequestDAO.loadShipAlertMails( null, shippingRequest);
            shippingRequestForm.setShippingRequest( shippingRequest );
        }
        catch( Exception e )
        {
            errors.put( "Could not retrive the Shipping Request", null );
            shippingRequestForm.setShippingRequest( new ShippingRequest() );
            //forwarding to the search page
            request.setAttribute( "vendorList", ShippingRequestDAO.loadClassStroeLocations( null ) );
            request.setAttribute( "partNumberList", ShippingRequestDAO.getProductList( Constants.LOCATION_TYPE_CLASS_STORE ) );
            request.setAttribute( "yearList", ShippingRequestDAO.getYearList() );
            request.setAttribute( "statusList", ShippingRequestDAO.getStatusList() );
            actionForward  = mapping.findForward( "search" );
        }
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        return actionForward;
    }
}
